package com.hibase.upms.service.dictdata.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import com.hibase.common.exception.HibaseAssert;
import com.hibase.upms.entity.dict.MdsDict;
import com.hibase.upms.entity.dictdata.MdsDictData;
import com.hibase.upms.entity.dictdata.vo.MdsDictDataSearchVO;
import com.hibase.upms.mapper.dictdata.MdsDictDataMapper;
import com.hibase.upms.service.dict.MdsDictService;
import com.hibase.upms.service.dictdata.MdsDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author chenfeng
 * @since 2021-03-12
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class MdsDictDataServiceImpl extends BaseServiceImpl<MdsDictDataMapper, MdsDictData> implements MdsDictDataService {

    @Autowired
    private MdsDictService mdsDictService;

    /**
     * 查询字典表列表
     */
    @Override
    public PageInfo<MdsDictData> selectMdsDictDataList(MdsDictDataSearchVO searchVO) {

        return super.page(searchVO, new QueryWrapper<MdsDictData>()
                .eq(StrUtil.isNotBlank(searchVO.getId()),"id",searchVO.getId())
                .eq(StrUtil.isNotBlank(searchVO.getDictId()),"dict_id",searchVO.getDictId())
        );
    }

    /**
     * 改变字典表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMdsDictData(MdsDictData entity) {
        super.store(entity);
    }

    /**
     * 查询字典表详情
     */
    @Override
    public MdsDictData viewMdsDictData(String id) {

        MdsDictData entity = super.getById(id, true);

        return entity;
    }

    /**
     * 删除字典表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMdsDictData(String id) {

        super.deleteBy(id);
        MdsDictData mdsDictData = super.getById(id);
    }

    @Override
    public List<MdsDictData> selectDictByCode(String dictType) {
    	HibaseAssert.isTrue(StrUtil.isNotBlank(dictType), " 数据字典不能为空");
        MdsDict mdsDict = mdsDictService.getOne(new QueryWrapper<MdsDict>().eq("dict_type", dictType));
        HibaseAssert.notNull(mdsDict,dictType.concat(" 数据字典不存在"));
        HibaseAssert.isTrue(mdsDict.getBeActive(),dictType.concat(" 数据字典未启用"));
        List<MdsDictData> list = super.list(new QueryWrapper<MdsDictData>()
                .eq("dict_id", mdsDict.getId())
                .eq("be_active", Boolean.TRUE));
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void enableMdsDictData(List<String> ids) {
        Collection<MdsDictData> dictDataList = super.listByIds(ids);
        dictDataList.forEach(dictData ->{
            HibaseAssert.isFalse(dictData.getBeActive(),"请勿重复操作");
            dictData.setBeActive(Boolean.TRUE);
        });
        super.storeBatch(dictDataList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disableMdsDictData(List<String> ids) {
        Collection<MdsDictData> dictDataList = super.listByIds(ids);
        dictDataList.forEach(dictData ->{
            HibaseAssert.isTrue(dictData.getBeActive(),"请勿重复操作");
            dictData.setBeActive(Boolean.FALSE);
        });
        super.storeBatch(dictDataList);
    }
}
